package com.hyt.it.ogt.kq.service.bm.pay;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.text.StrPool;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author yinyachuan
 * @create 2018-03-05 下午9:24
 * @desc
 **/
public class MapUtils {

	

	public static <K, V> Map<K, V> transform(Collection<V> collections, Function<K, V> function) {
		Map<K, V> map = new LinkedHashMap<K, V>();

		for (V collection : collections) {
			K apply = function.apply(collection);
			map.put(apply, collection);
		}
		return map;
	}

	public interface Function<K, V> {
		K apply(V var1);
	}

	public static String createLinkStringByGet(Map<String, Object> params, int isPage, Integer pageIndex,
			Integer pageSize) {
		String prestr = "";
		try {
			List<String> keys = new ArrayList<String>(params.keySet());
			Collections.sort(keys);
			for (int i = 0; i < keys.size(); i++) {
				String key = keys.get(i);
				if (null != params.get(key)) {
					String value = params.get(key).toString();
					if (StringUtils.isNotBlank(value)) {
						value = URLEncoder.encode(value, "UTF-8");
						prestr = prestr + key + "=" + value + "&";
					}
				}
			}
			if (0 == isPage) {
				prestr = prestr + "limit=" + pageIndex + StrPool.COMMA + pageSize;
			}
			if (prestr.endsWith("&")) {
				prestr = prestr.substring(0, prestr.length() - 1);
			}

		} catch (Exception e) {
			//  Auto-generated catch block
			e.printStackTrace();
		}
		return prestr;
	}

	/**
	 * 实体对象转成Map
	 *
	 * @param obj 实体对象
	 * @return
	 */
	public static Map<String, Object> object2MapObj(Object obj) {
		Map<String, Object> map = new HashMap<>();
		if (obj == null) {
			return map;
		}
		Class<? extends Object> clazz = obj.getClass();
		Field[] fields = clazz.getDeclaredFields();
		try {
			for (Field field : fields) {
				field.setAccessible(true);
				map.put(field.getName(), field.get(obj));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static Map<String, String> object2MapStr(Object obj){
		return valueObject2String(object2MapObj(obj));
	}
	/**
	 * Map转成实体对象
	 *
	 * @param map   实体对象包含属性
	 * @param clazz 实体对象类型
	 * @return
	 */
	public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
		if (map == null) {
			return null;
		}
		Object obj = null;
		try {
			obj = clazz.newInstance();

			Field[] fields = obj.getClass().getDeclaredFields();
			for (Field field : fields) {
				int mod = field.getModifiers();
				if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
					continue;
				}
				field.setAccessible(true);
				field.set(obj, map.get(field.getName()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}



	/**
	 * 将map中的value由object转string
	 * 
	 * @param map Map<String, Object>
	 * @return
	 */
	public static Map<String, String> valueObject2String(Map<String, Object> map) {
		Map<String, String> returnMap = new HashMap<String, String>();
		for (String s : map.keySet()) {
			if (map.get(s) == null) {
				returnMap.put(s, "");
			} else {
				returnMap.put(s, map.get(s).toString());
			}
		}
		return returnMap;

	}

	/**
	 * 将map中的value中的null转"",其他参数转为string类型
	 * 
	 * @param nullMap
	 * @return
	 */
	public static Map<String, String> null2Empty(Map<String, Object> nullMap) {
		Map<String, String> respMap = new HashMap<String, String>();
		for (Map.Entry<String, Object> mapEntry : nullMap.entrySet()) {
			if (mapEntry.getValue() == null) {
				respMap.put(mapEntry.getKey(), "");
			} else {
				respMap.put(mapEntry.getKey(), mapEntry.getValue().toString());
			}
		}
		return respMap;
	}
}

